package com.gec.zhs.controller;

import com.gec.zhs.annotation.ZhsLogin;
import com.gec.zhs.entity.constants.Constants;
import com.gec.zhs.entity.dto.TokenUserInfoDto;
import com.gec.zhs.entity.enums.PageSize;
import com.gec.zhs.entity.enums.UserActionTypeEnum;
import com.gec.zhs.entity.enums.VideoOrderTypeEnum;
import com.gec.zhs.entity.pojo.UserAction;
import com.gec.zhs.entity.pojo.UserFocus;
import com.gec.zhs.entity.pojo.UserInfo;
import com.gec.zhs.entity.pojo.VideoInfo;
import com.gec.zhs.entity.query.UserActionQuery;
import com.gec.zhs.entity.query.UserFocusQuery;
import com.gec.zhs.entity.query.VideoInfoQuery;
import com.gec.zhs.entity.vo.PaginationResultVO;
import com.gec.zhs.entity.vo.ResponseVO;
import com.gec.zhs.entity.vo.UserInfoVO;
import com.gec.zhs.service.UserActionService;
import com.gec.zhs.service.UserFocusService;
import com.gec.zhs.service.UserInfoService;
import com.gec.zhs.service.VideoInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhs
 * @version 1.0
 * @date 2025-02-22
 * @description 个人主页
 */
@RestController
@RequestMapping("/uhome")
public class UserHomeController extends ABaseController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private VideoInfoService videoInfoService;

    @Autowired
    private UserFocusService userFocusService;

    @Autowired
    private UserActionService userActionService;


    @ApiOperation("获取用户个人信息")
    @RequestMapping("/getUserInfo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO getUserInfoByUid(String userId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userDetailInfo = userInfoService.getUserDetailInfo(tokenUserInfoDto.getUserId(), userId);
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userDetailInfo, userInfoVO);
        return getSuccessResponseVO(userInfoVO);
    }

    @ApiOperation("修改用户个人信息")
    @RequestMapping("/updateUserInfo")
    @ZhsLogin(checkLogin = true)
    public ResponseVO updateUserInfo(String nickName, String avatar, Integer sex, String birthday,
                                     String school, String personIntroduction, String noticeInfo){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(tokenUserInfoDto.getUserId());
        userInfo.setNickName(nickName);
        userInfo.setAvatar(avatar);
        userInfo.setSex(sex);
        userInfo.setBirthday(birthday);
        userInfo.setSchool(school);
        userInfo.setPersonIntroduction(personIntroduction);
        userInfo.setNoticeInfo(noticeInfo);

        userInfoService.updateUserInfo(userInfo, tokenUserInfoDto);

        return getSuccessResponseVO(null);
    }

    @ApiOperation("保存主题")
    @RequestMapping("/saveTheme")
    @ZhsLogin(checkLogin = true)
    public ResponseVO saveTheme(Integer theme) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userInfo = new UserInfo();
        userInfo.setTheme(theme);
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }


    @ApiOperation("关注UP主")
    @RequestMapping("/focus")
    @ZhsLogin(checkLogin = true)
    public ResponseVO focus(String focusUserId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userFocusService.focusUser(tokenUserInfoDto.getUserId(),focusUserId);
        return getSuccessResponseVO(null);
    }

    @ApiOperation("取消关注")
    @RequestMapping("/cancelFocus")
    @ZhsLogin(checkLogin = true)
    public ResponseVO cancelFocus(String focusUserId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userFocusService.cancelFocus(tokenUserInfoDto.getUserId(),focusUserId);
        return getSuccessResponseVO(null);
    }

    @ApiOperation("加载关注人的信息")
    @RequestMapping("/loadFocusList")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadFocusList(Integer pageNo){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserFocusQuery userFocusQuery = new UserFocusQuery();
        userFocusQuery.setUserId(tokenUserInfoDto.getUserId());
        userFocusQuery.setPageNo(pageNo);
        userFocusQuery.setOrderBy("focus_time desc");
        //0 - > queryType - > focus
        userFocusQuery.setQueryType(Constants.ZERO);
        PaginationResultVO<UserFocus> listByPage = userFocusService.findListByPage(userFocusQuery);
        return getSuccessResponseVO(listByPage);
    }

    @ApiOperation("查询粉丝信息")
    @RequestMapping("/loadFansList")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadFansList(Integer pageNo){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserFocusQuery userFocusQuery = new UserFocusQuery();
        userFocusQuery.setFocusUserId(tokenUserInfoDto.getUserId());
        // 1 - > queryType - > 粉丝
        userFocusQuery.setQueryType(Constants.ONE);
        userFocusQuery.setPageNo(pageNo);
        userFocusQuery.setOrderBy("focus_time desc");

        PaginationResultVO<UserFocus> listByPage = userFocusService.findListByPage(userFocusQuery);
        return getSuccessResponseVO(listByPage);
    }

    @ApiOperation("个人首页加载视频")
    @RequestMapping("/loadVideoList")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadVideoList(String userId, Integer type, Integer pageNo, String videoName, Integer orderType) {
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        if (type != null) {
            videoInfoQuery.setPageSize(PageSize.SIZE10.getSize());
        }
        VideoOrderTypeEnum videoOrderTypeEnum = VideoOrderTypeEnum.getByType(orderType);
        if (videoOrderTypeEnum == null) {
            videoOrderTypeEnum = VideoOrderTypeEnum.CREATE_TIME;
        }
        videoInfoQuery.setOrderBy(videoOrderTypeEnum.getField() + " desc");
        videoInfoQuery.setVideoNameFuzzy(videoName);
        videoInfoQuery.setPageNo(pageNo);
        videoInfoQuery.setUserId(userId);
        PaginationResultVO<VideoInfo> listByPage = videoInfoService.findListByPage(videoInfoQuery);
        return getSuccessResponseVO(listByPage);
    }

    @ApiOperation("获取用户的收藏内容")
    @RequestMapping("/loadUserCollection")
    @ZhsLogin(checkLogin = true)
    public ResponseVO loadUserCollection(String userId, Integer pageNo) {
        UserActionQuery userActionQuery = new UserActionQuery();
        userActionQuery.setActionType(UserActionTypeEnum.VIDEO_COLLECT.getType());
        userActionQuery.setUserId(userId);
        userActionQuery.setPageNo(pageNo);
        userActionQuery.setQueryVideoInfo(true);
        userActionQuery.setOrderBy("action_time desc");
        PaginationResultVO<UserAction> listByPage = userActionService.findListByPage(userActionQuery);
        return getSuccessResponseVO(listByPage);
    }

}
